package com.herui.sdyu_lib.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public abstract class SimpleBaseAdapter<T> extends BaseAdapter {

	protected List<T> list;

	protected Context context;

	public SimpleBaseAdapter(Context context) {
		init(context, new ArrayList<T>());
	}

	public SimpleBaseAdapter(Context context, List<T> list) {
		init(context, list);
	}

	private void init(Context context, List<T> list) {
		this.list = list;
		this.context = context;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public void clear() {
		this.list.clear();
		notifyDataSetChanged();
	}

	public void addAll(List<T> list) {
		if (list != null) {
			this.list.addAll(list);
			notifyDataSetChanged();
		}
	}

	@Override
	public int getCount() {
		return list == null ? 0 : list.size();
	}

	@Override
	public T getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		return convertView;
	}

	/** 加载布局 */
	public View inflate(int layoutResID) {
		LayoutInflater layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = layoutInflater.inflate(layoutResID, null);
		return view;
	}
	
	public void refreshData(){
		this.notifyDataSetChanged();
	}
	
	public void refreshData(List<T> list){
		this.list = list;
		this.notifyDataSetChanged();
	}
}
